﻿#ifndef SWANLINK_FINANCE_CARDSENDER_ADAPTOR_F5G006_LIB_LIBF5G006_INCLUDE_TIMEOUT_WATCH_H
#define SWANLINK_FINANCE_CARDSENDER_ADAPTOR_F5G006_LIB_LIBF5G006_INCLUDE_TIMEOUT_WATCH_H

#include <chrono>

class TimeoutWatch {
 private:
    typedef std::chrono::steady_clock clock_t;
    typedef clock_t::time_point tick_t;
    tick_t m_t0;
    long m_timeoutVal;

 public:
    inline TimeoutWatch(long timeoutVal)
        : m_timeoutVal(timeoutVal) {
        m_t0 = clock_t::now();
    }

    inline long elapsedTime() const {
        return (long)std::chrono::duration_cast<std::chrono::milliseconds>(clock_t::now() - m_t0).count();
    }

    inline long remainingTime() const {
        long diff = m_timeoutVal - elapsedTime();
        return diff < 0 ? 0 : diff;
    }

    inline bool isTimeOut() const {
        return elapsedTime() > m_timeoutVal;
    }
};

#endif  // SWANLINK_FINANCE_CARDSENDER_ADAPTOR_F5G006_LIB_LIBF5G006_INCLUDE_TIMEOUT_WATCH_H
